﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;

namespace Plugins.Library
{
    [Serializable]
    public static class PluginBitmapHelper
    {
        public static Bitmap GetTransparentBitmap(Image image, Color color) 
        {
            Bitmap bitmap = new Bitmap(image);
            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    Color src = bitmap.GetPixel(i, j);
                    float alpha = (6f - (Math.Abs(src.R - color.R) + Math.Abs(src.G - color.G) + Math.Abs(src.B - color.B)) / 255f) / 6f;
                    Color dst = Color.FromArgb(GetValue(alpha, 0, 255), src.R, src.G, src.B);
                    bitmap.SetPixel(i, j, dst);
                }
            }
            return bitmap;
        }

        [SuppressMessage("Microsoft.Naming", "CA1704")]
        public static int GetValue(float alpha, int p1, int p2)
        {
            double a = Math.Pow(alpha, 32);
            return (int)(Math.Round(a * p1 + (1 - a) * p2));
        }

        [SuppressMessage("Microsoft.Naming", "CA1704")]
        public static float Dot(float x1, float y1, float z1, float x2, float y2, float z2)
        {
            return x1 * x2 + y1 * y2 + z1 * z2;
        }
    }
}
